package com.qk.controller;

import com.qk.aop.anno.LogRecord;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.constant.BusinessStatusConstants;
import com.qk.domain.business.BusinessDO;
import com.qk.dto.business.BusinessDTO;
import com.qk.dto.business.BusinessFollowUpdateDTO;
import com.qk.entity.Business;
import com.qk.service.BusinessService;
import com.qk.vo.business.BusinessVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: zhangchuang
 * @Date: 2025/10/15 20:47
 * @Description: 商机控制器
 */
@RestController
@Slf4j
@RequestMapping("/businesses")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    @GetMapping("/pool")
    public Result getCluePool(BusinessDTO clueQueryDTO) {
        log.info("查询线索池, {}", clueQueryDTO);
        clueQueryDTO.setStatus(BusinessStatusConstants.RECOVERY);
        return getBusinessByPage(clueQueryDTO);
    }

    @LogRecord
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
        log.info("转成客户, {}", id);
        return businessService.toCustomer(id) ? Result.success() : Result.error();
    }

    @LogRecord
    @PutMapping("/back/{id}")
    public Result backBusiness(@PathVariable Integer id) {
        log.info("退回商机, {}", id);
        return businessService.backBusiness(id) ? Result.success() : Result.error();
    }

    @LogRecord
    @PutMapping
    public Result updateBusinessInfo(@RequestBody BusinessFollowUpdateDTO dto) {
        log.info("更新商机信息, {}", dto);
        return businessService.update(dto) ? Result.success() : Result.error();
    }

    @LogRecord
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assignBusiness(@PathVariable Integer businessId, @PathVariable Integer userId) {
        log.info("分配商机, 商机id: {}, 分配给: {}", businessId, userId);
        return businessService.assignBusiness(businessId, userId) ? Result.success() : Result.error();
    }

    @GetMapping("/{id}")
    public Result getBusinessInfoById(@PathVariable Integer id) {
        log.info("查询商机信息, 商机id: {}", id);
        BusinessVO vo = businessService.getBusinessInfoById(id);
        return Result.success(vo);
    }

    @LogRecord
    @PostMapping
    public Result addBusiness(@RequestBody Business business) {
        log.info("添加商机, {}", business);
        return businessService.addBusiness(business) ? Result.success() : Result.error();
    }

    @GetMapping
    public Result getBusinessByPage(BusinessDTO businessDTO) {
        log.info("查询商机, {}", businessDTO);
        PageResult<BusinessDO> pageResult = businessService.getBusinessByPage(businessDTO);
        return Result.success(pageResult);
    }

}
